Skip to content

KTOR-8200 fix + encoding on appendPathSegments #4738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hungrytech
Copy link
Contributor

Subsystem
ktor-http

Motivation
KTOR-8200

Solution
Removed the '+' character from VALID_PATH_PART.

Copy link

coderabbitai bot commented Mar 15, 2025

Walkthrough

The changes update how URLs are processed by removing the '+' character from the set of valid path characters in the URL codec. This modification affects URL parsing, causing the '+' to be encoded as %2B rather than retained literally. The test expectations were updated accordingly in the URL builder test. No public API declarations were altered.

Changes

File(s) Change Summary
ktor-http/.../io/ktor/http/Codecs.kt
ktor-http/.../io/ktor/tests/http/URLBuilderTest.kt
Removed the '+' character from the valid URL path characters in the codec and updated the test to expect %2B encoding instead of a literal + value.

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 31bfd9d and 4842d83.

📒 Files selected for processing (2)
  • ktor-http/common/src/io/ktor/http/Codecs.kt (1 hunks)
  • ktor-http/common/test/io/ktor/tests/http/URLBuilderTest.kt (1 hunks)
🔇 Additional comments (2)
ktor-http/common/src/io/ktor/http/Codecs.kt (1)

27-31:

✅ Verification successful

Correctly removed '+' from VALID_PATH_PART to align with RFC 3986

This change ensures that the '+' character will now be properly percent-encoded as %2B in URL paths, which aligns with RFC 3986. According to the specification, the unreserved characters are limited to alphanumerics, hyphen, period, underscore, and tilde.


🏁 Script executed:

#!/bin/bash
# Verify how this change affects URL encoding in the codebase

# Check if there are any other usages of VALID_PATH_PART in the codebase
echo "Checking for other usages of VALID_PATH_PART:"
rg "VALID_PATH_PART" --type kotlin

# Check for any tests that might rely on '+' not being encoded in paths
echo "Checking for tests that might be affected:"
rg "\\+" --type kotlin --context 5 | grep -i "test" | grep -i "url"

Length of output: 37283


RFC Alignment Confirmed – URL percent‐encoding behavior is now correct

The change in ktor-http/common/src/io/ktor/http/Codecs.kt (lines 27–31) removes the '+' character from the set of valid path characters, ensuring that when raw URL parts are encoded (using functions like encodeURLPath()), a '+' is percent-encoded as %2B per RFC 3986. Verification through tests shows that:

  • In core encoding (e.g. in URLBuilderTest.kt’s testPathEncoding), a segment like "id+test&test~test#test" is correctly transformed to "id%2Btest&test~test%23test".
  • Meanwhile, in contexts where the API expects pre‐encoded segments (e.g. the server builders using path("a+b", "c") or appendPathSegments("a+b", "c")), literal plus signs are preserved, which is consistent with their intended use.

Thus, the behavior now aligns with RFC 3986 while maintaining expected outcomes in different URL-building contexts.

ktor-http/common/test/io/ktor/tests/http/URLBuilderTest.kt (1)

274-274: Test expectation correctly updated to reflect new '+' encoding behavior

The test expectation was properly updated to verify that the '+' character is now encoded as %2B in URL paths, which is consistent with the change made to the VALID_PATH_PART set in the Codecs.kt file.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please look into fixing these tests? It looks like it might just be a case of bad assertions, unless if you find some reason that they ought to expect the previous behaviour.

https://ktor.teamcity.com/buildConfiguration/Ktor_KtorMatrixCoreLinuxJava21/325088

@hungrytech
Copy link
Contributor Author

@bjhham
Yes, I agree with what you mentioned. According to RFC 3986, encoding may not be necessary. It seems that only the query part needs to be encoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants